$('.downadd').click(function(){
	var provinceid = $(this).attr('provinceid');
	var this_class = $(this).attr('class');
	var this_ = $(this);
	if(provinceid==0){
		ajax({
			'url':'/admin/login/region',
			'type':'post',
			'data':{},
			'dataType':'json',
			'success':function(response) {
				if(response.success=='OK'){
					var data = response.data;
					var data_sheng = [];
					if($(this_).attr('aore')=='a'){
						var sheng = '#sheng';
						var shi = '#shi';
					}else if($(this_).attr('aore')=='e'){
						var sheng = '#city_s';
						var shi = '#city';
					}
					for (var i = 0; i < data.length; i++) {
						$(sheng).append($('<option value="'+data[i].id+'">'+data[i].name+'</option>'));
						data_sheng[data[i].id] = data[i];
					}
					$(sheng).on('change',function(){
						var thisid = $(this).val();
						var shi_ = data_sheng[thisid].shi;
						var provinceid_shi = $(shi).attr('provinceid');
						if(provinceid_shi==thisid){
							return false;
						}
						$(shi).attr('provinceid',thisid);
						$(shi).empty();
						$(shi).append($('<option value="0" disabled="disabled" selected="selected">请选择</option>'));
						for (var i = 0; i < shi_.length; i++) {
							$(shi).append($('<option value="'+shi_[i].id+'">'+shi_[i].name+'</option>'));
						}
					});
					$(this_).attr('provinceid','1');
				}else{

				}
			}
		});
	}
});


function selectcity(this_1) {
	var provinceid = $(this_1).attr('provinceid');
	var this_class = $(this_1).attr('class');
	$(this_1).html('确认');
	$(this_1).attr('onclick','editinfo(this)');
	$(this_1).parents('tr').find('.tpl_s span').hide();
	$(this_1).parents('tr').find('.showp').show();
	var this_ = $(this_1);
	if(provinceid=='0'){
		ajax({
			'url':'/admin/login/region',
			'type':'post',
			'data':{},
			'dataType':'json',
			'success':function(response) {
				if(response.success=='OK'){
					var data = response.data;
					var data_sheng = [];
					var sheng = $(this_1).parents('tr').find('select[name="province_edit"]');
					var shi = $(this_1).parents('tr').find('select[name="city_edit"]');
					for (var i = 0; i < data.length; i++) {
						if(sheng.attr('idc')==data[i].id){
							sheng.append($('<option value="'+data[i].id+'" selected>'+data[i].name+'</option>'));
						}else{
							sheng.append($('<option value="'+data[i].id+'">'+data[i].name+'</option>'));
						}
						data_sheng[data[i].id] = data[i];
					}
					var thisid = sheng.attr('idc');
					var shi_ = data_sheng[thisid].shi;
					var provinceid_shi = shi.attr('provinceid');
					if(provinceid_shi==thisid){
						return false;
					}
					shi.attr('provinceid',thisid);
					shi.empty();
					for (var i = 0; i < shi_.length; i++) {
						if(shi.attr('idc')==data[i].id){
							shi.append($('<option value="'+shi_[i].id+'" selected>'+shi_[i].name+'</option>'));
						}else{
							shi.append($('<option value="'+shi_[i].id+'">'+shi_[i].name+'</option>'));
						}
					}
					sheng.on('change',function(){
						var thisid = $(this).val();
						var shi_ = data_sheng[thisid].shi;
						var provinceid_shi = shi.attr('provinceid');
						if(provinceid_shi==thisid){
							return false;
						}
						shi.attr('provinceid',thisid);
						shi.empty();
						for (var i = 0; i < shi_.length; i++) {
							if(shi.attr('idc')==data[i].id){
								shi.append($('<option value="'+shi_[i].id+'" selected>'+shi_[i].name+'</option>'));
							}else{
								shi.append($('<option value="'+shi_[i].id+'">'+shi_[i].name+'</option>'));
							}
						}
					});
					$(this_).attr('provinceid','1');
				}else{

				}
			}
		});
	}
}

function editinfo(this_) {
	var isedit = false;
	var parentr = $(this_).parents('tr');
	var phone = parentr.find('input[name="phone_edit"]').val().trim();
	var phone_html = parentr.find('.phone_edit_html').html().trim();
	var auth = parentr.find('select[name="auth_edit"]').val();
	var auth_defaultid = parentr.find('select[name="auth_edit"]').attr('defaultid');
	var province = parentr.find('select[name="province_edit"]').val();
	var province_idc = parentr.find('select[name="province_edit"]').attr('idc');
	var city = parentr.find('select[name="city_edit"]').val();
	var city_idc = parentr.find('select[name="city_edit"]').attr('idc');
	var userid = parentr.find('input[name="userid"]').val();
	var phone_reg = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/i;
	if(!phone_reg.test(phone)){
		sweetAlert("","手机格式不正确","error");
		return false;
	}
	if(!auth||auth==0){
		sweetAlert("","请选择管理员职务！","error");
		return false;
	}
	if(!province||province==0){
		sweetAlert("","请选择管理员所属省！","error");
		return false;
	}
	if(!city||city==0){
		sweetAlert("","请选择管理员所属市！","error");
		return false;
	}
	var postdata = {};
	if(phone_html!=phone){
		postdata.phone = phone;
		isedit = true;
	}
	if(auth!=auth_defaultid){
		isedit = true;
	}
	if(province!=province_idc){
		isedit = true;
	}
	if(city!=city_idc){
		isedit = true;
	}
	if(!isedit){
		$(this_).html('编辑');
		$(this_).attr('onclick','selectcity(this)');
		parentr.find('.tpl_s span').show();
		parentr.find('.showp').hide();
		sweetAlert("","您没有修改任何数据,请刷新重试！","error");
		return false;
	}
	postdata.auth = auth;
	postdata.province = province;
	postdata.city = city;
	postdata.userid = userid;
	post('/Admin/login/EditAdmin',postdata,function(response){
		if(response.error>0){
			sweetAlert("",response.message,"error");
		}else if(response.success=='OK'){
			parentr.find('.phone_edit_html').html(postdata.phone);
			parentr.find('.positionauth').html(parentr.find('select[name="auth_edit"]').find('option[value="'+postdata.auth+'"]').html());
			parentr.find('.provinceidname').html(parentr.find('select[name="province_edit"]').find('option[value="'+postdata.province+'"]').html());
			parentr.find('.cityname').html(parentr.find('select[name="city_edit"]').find('option[value="'+postdata.city+'"]').html());
			$(this_).html('编辑');
			$(this_).attr('onclick','selectcity(this)');
			parentr.find('.tpl_s span').show();
			parentr.find('.showp').hide();
			sweetAlert("",response.message,"success");
		}else{
			sweetAlert("","数据返回错误，请联系开发人员！","error");
		}
	},'json');

}

$('#ActionAddAdmin').ajaxForm({
	'beforeSubmit':function() {
		var username_reg = /^[\u4E00-\u9FA5A-Za-z]+$/;
		var phone_reg = /^(13[0-9]|14[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/i;
		var username = $('input[name="username"]').val().trim();
		var phone = $('input[name="phone"]').val().trim();
		var auth = $('select[name="auth"]').val();
		var usernum = $('input[name="usernum"]').val();
		var province = $('#sheng').val();
		var city = $('#shi').val();
		if(!username_reg.test(username)){
			sweetAlert("","姓名格式不正确！","error");
			return false;
		}
		if(!usernum||usernum==""){
			sweetAlert("","请输入员工编号！","error");
			return false;
		}
		if(!phone_reg.test(phone)){
			sweetAlert("","手机格式不正确！","error");
			return false;
		}
		if(!auth||auth==0){
			sweetAlert("","请选择管理员职务！","error");
			return false;
		}
		if(!province||province==0){
			sweetAlert("","请选择管理员所属省！","error");
			return false;
		}
		if(!city||city==0){
			sweetAlert("","请选择管理员所属市！","error");
			return false;
		}
	},
	'success':function(response) {
		if(response.error>0){
			sweetAlert("",response.message,"error");
		}else if(response.success=='OK'){
			$(".modal_t").hide();
			location.href = location.href;
			sweetAlert("",response.message,"success");

		}else{
			sweetAlert("","数据返回错误，请联系开发人员！","error");
		}
	},
	'dataType':'json'
});